import {
    LOGIN_SUCCESS,
    USERINFO_SUCCESS,
    USERINFO_FAILURE,
    LOGOUT_USER,
    UPDATE_USER_SUCCESS
} from '../types'
let token,user=null;
if (localStorage.token){
    token=JSON.parse(localStorage.token);
}
if (localStorage.user){
    user=JSON.parse(localStorage.user);
}
const state = {
    token: token,
    user: user
};

const mutations = {
    [LOGIN_SUCCESS](state , action){
        state.token = action.token
    },
    [USERINFO_SUCCESS](state,action){
        state.user = action.user;
        console.log(state.user);
    },
    [USERINFO_FAILURE](state,action){
        state.user = null
    },
    [LOGOUT_USER](state,action){
        localStorage.removeItem("token");
        state.user = null;
        state.token = null;
    },
    [UPDATE_USER_SUCCESS](state,action){
        state.user = action.user
    }
};



export default {
    state,
    mutations
}